﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Order.Entity;
using Runda.CMS.Order.Repository;
using Runda.CMS.Common.Utility;

namespace Runda.CMS.Portal.OrderManagement
{
    public partial class OrderShipping : BasePage
    {
        private ShippingRequestRepository shippingRequestRepository = new ShippingRequestRepository();

        private ShippingRepository shippingRepository = new ShippingRepository();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) {
                if (Request.QueryString["orderId"] != String.Empty && Request.QueryString["shippingRequestId"] != String.Empty)
                {
                    String requestId = Request.QueryString["shippingRequestId"];
                    List<ShippingRequest> unshippingRequests = shippingRequestRepository.getUnShippingRequests(Request.QueryString["orderId"]);
                    List<ShippingRequest> requests = new List<ShippingRequest>();
                    foreach (ShippingRequest request in unshippingRequests) {
                        if (requestId == request.requestId) {
                            requests.Add(request);
                        }
                    }
                    shippingRequests.DataSource = requests;
                    shippingRequests.DataBind();
                    RepeaterShippings.DataSource = shippingRepository.findShippingByOrderId(Request.QueryString["orderId"]);
                    RepeaterShippings.DataBind();
                }
            }
        }

        protected void RepeaterShippings_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            System.Web.UI.HtmlControls.HtmlGenericControl requestShippingId = e.Item.FindControl("requestShippingId") as System.Web.UI.HtmlControls.HtmlGenericControl;
            Label sendBillNo = e.Item.FindControl("sendBillNo") as Label;
            if (sendBillNo != null)
            {
                List<ShippingRequest> requests = shippingRepository.findShippingRequestByShippingId(sendBillNo.Text, Request.QueryString["orderId"]);
                foreach (ShippingRequest request in requests)
                {
                    requestShippingId.InnerHtml = requestShippingId.InnerHtml + "<a target='_blank' href='RequestDetail.aspx?shippingRequestId=" + request.requestId + "'>" + request.requestId + "</a>&nbsp;";
                }
            }
        }
        
        protected void lnkbtnSave_Click(object sender, EventArgs e)
        {
            Save();
            ClientScript.RegisterStartupScript(this.GetType(), "saveandclose", "alert('保存成功');", true);
        }

        protected void lnkbtnSaveAndClose_Click(object sender, EventArgs e)
        {
            Save();
            ClientScript.RegisterStartupScript(this.GetType(), "saveandclose", "window.close();", true);
        }      
        
        private void Save()
        {
            List<Shipping> shippings = shippingRepository.findShippingByBillNumAndSendCompany(txtSendBillNo.Text.Trim(), txtSendCompany.Text.Trim());
            if (shippings != null && shippings.Count > 0) {
                ClientScript.RegisterStartupScript(this.GetType(), "saveandclose", "alert('该单据已存在');", true);
                return;
            }
            for (int index = 0; index < shippingRequests.Items.Count; index++) {
                CheckBox shippingRequestCheck = shippingRequests.Items[index].FindControl("shippingRequestCheck") as CheckBox;
                HyperLink requestNum = shippingRequests.Items[index].FindControl("requestNum") as HyperLink;
                if (shippingRequestCheck.Checked) {                                  
                    List<ShippingRequestDetail> details = shippingRequestRepository.getDetailByRequestId(requestNum.Text.Trim());
                    using (System.Transactions.TransactionScope trans = new System.Transactions.TransactionScope())
                    {
                        ShippingRequestDetail sdetail = details[0];

                        Shipping shipping = new Shipping();
                        shipping.createdBy = base.CurrentUserID;
                        shipping.createdOn = DateTime.Now;
                        shipping.updatedOn = DateTime.Now;
                        shipping.updatedBy = base.CurrentUserID;
                        shipping.customerID = sdetail.customerId;
                        //shipping.inventoryCode = detail.inventoryId;
                        shipping.orderId = sdetail.orderID;
                        if (txtPreReceivedDate.SelectedDate != null)
                        {
                            shipping.preReceivedDate = txtPreReceivedDate.SelectedDate.Value;
                        }
                        //shipping.quantity = detail.quantity;
                        shipping.sendBillNo = txtSendBillNo.Text;
                        shipping.sendCompany = txtSendCompany.Text;
                        shipping.shippingRequestId = sdetail.requestID;
                        shippingRepository.persist(shipping);


                        //foreach (ShippingRequestDetail detail in details)
                        //{
                        //    Shipping shipping = new Shipping();
                        //    shipping.createdBy = base.CurrentUserID;
                        //    shipping.createdOn = DateTime.Now;
                        //    shipping.updatedOn = DateTime.Now;
                        //    shipping.updatedBy = base.CurrentUserID;
                        //    shipping.customerID = detail.customerId;
                        //    shipping.inventoryCode = detail.inventoryId;
                        //    shipping.orderId = detail.orderID;
                        //    if(txtPreReceivedDate.SelectedDate != null){
                        //        shipping.preReceivedDate = txtPreReceivedDate.SelectedDate.Value;
                        //    }
                        //    shipping.quantity = detail.quantity;
                        //    shipping.sendBillNo = txtSendBillNo.Text;
                        //    shipping.sendCompany = txtSendCompany.Text;
                        //    shipping.shippingRequestId = detail.requestID;                           
                        //    shippingRepository.persist(shipping);
                        //}
                        trans.Complete();
                    }
                }
            }
        }
    }
}
